<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>招聘信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>招聘信息列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="招聘岗位/企业名称"
            class="input-with-select"
            v-model="queryInfo.querystring"
            clearable
            @clear="getrecruitlist"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getrecruitlist"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加招聘信息</el-button>
        </el-col>
      </el-row>
      <!-- 招聘列表区域 -->
      <el-table :data="recruitlist"  style="width: 100%" >
        <el-table-column type="expand" prop="jobrequirements">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="岗位描述">
                <span v-html="props.row.jobrequirements"></span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="companyid" sortable label="企业编号">
        </el-table-column>
        <el-table-column prop="companyname" sortable label="企业名称">
        </el-table-column>
        <el-table-column prop="principal" sortable label="负责人">
        </el-table-column>
        <el-table-column prop="phone" sortable label="电话"> </el-table-column>
        <el-table-column prop="jobs" sortable label="招聘岗位">
        </el-table-column>
        <el-table-column prop="numbers" sortable label="招聘人数">
        </el-table-column>
        <el-table-column prop="catename" sortable label="岗位类别">
        </el-table-column>
        <el-table-column prop="eduname" sortable label="学历">
        </el-table-column>
        <el-table-column prop="legal" sortable label="审核">
          <template slot-scope="scope">
            <span v-if="scope.row.legal==0">未通过</span>
            <span v-if="scope.row.legal==1">通过</span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="jobrequirements" sortable label="岗位要求">
          <template slot-scope="scope">
            <div v-html="scope.row.jobrequirements"></div>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮-->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUserById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!--修改招聘的对话框  -->
    <el-dialog
      title="修改招聘"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="招聘岗位" prop="phone">
          <el-input v-model="editForm.jobs"></el-input>
        </el-form-item>
        <el-form-item label="招聘人数" prop="numbers">
          <el-input v-model="editForm.numbers"></el-input>
        </el-form-item>
        <el-form-item label="薪资" prop="salary">
          <el-input v-model="editForm.salary"></el-input>
        </el-form-item>
        <el-form-item label="工作地点" prop="placename">
          <el-cascader
            expand-trigger="hover"
            :options="workplace"
            :props="workplaceProps"
            v-model="editForm.placeids"
            @change="handleChange"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="岗位类别" prop="catename">
          <el-cascader
            expand-trigger="hover"
            :options="catelist"
            :props="cateProps"
            v-model="editForm.cateids"
            @change="handleChange"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="学历" prop="eduid">
          <el-select v-model="editForm.eduid" placeholder="请选择">
            <el-option
              v-for="item in edulist"
              :key="item.eduname"
              :label="item.eduname"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="岗位要求" prop="jobrequirements">
          <!-- 富文本编辑器组件 -->
          <quill-editor v-model="editForm.jobrequirements"></quill-editor>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editrecruitInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import _ from 'lodash'
export default {
  data() {
    // 验证手机号的规则
    var checkphone = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regphone =
        /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
      if (regphone.test(value)) {
        return cb()
      }
      cb(new Error('请输入合法的手机号'))
    }
    return {
      //获取招聘列表的参数
      queryInfo: {
        querystring: '',
        pagenum: 1,
        pagesize: 2,
      },
      recruitlist: [],
      total: 0,
      // 岗位分类列表
      catelist: [],
      workplace: [],
      cateProps: {
        label: 'catename',
        value: 'id',
        children: 'children',
      },
      workplaceProps: {
        label: 'placename',
        value: 'id',
        children: 'children',
      },
      eduProps: {
        label: 'eduname',
        value: 'id',
      },
      edulist: [],
      //控制修改招聘对话框的显示与隐藏
      editDialogVisible: false,
      //查询到的招聘信息对象
      editForm: {},
      //修改表单的验证规则
      editFormRules: {
        phone: [
          { required: true, message: '请输入手机', trigger: 'blur' },
          { validator: checkphone, trigger: 'blur' },
        ],
      },
    }
  },
  created() {
    this.getrecruitlist(),
      this.getCateList(),
      this.educationlist(),
      this.getWorkplaceList()
  },
  methods: {
    // 获取所有岗位分类数据
    async getCateList() {
      const { data: res } = await this.$http.get('categories')

      if (res.meta.status !== 200) {
        return this.$message.error('获取岗位分类数据失败')
      }
      this.catelist = res.data
      console.log(this.catelist)
    },
    // 获取所有工作地点分类数据
    async getWorkplaceList() {
      const { data: res } = await this.$http.get('workplace')

      if (res.meta.status !== 200) {
        return this.$message.error('获取工作地点数据失败')
      }
      this.workplace = res.data
    },
    // 获取所有学历分类数据
    async educationlist() {
      const { data: res } = await this.$http.get('educations')
      if (res.meta.status !== 200) {
        return this.$message.error('获取学历数据失败')
      }
      this.edulist = res.data
    },
    // 级联选择器选中项变化，会触发这个函数
    handleChange() {
      
    },
    //获取所有岗位
    async getrecruitlist() {
        this.user = JSON.parse(window.sessionStorage.getItem('user'))
      const { data: res } = await this.$http.get('getRecruitByComId/'+ this.user.actorid, {
        params: this.queryInfo,
      })
      if (res.meta.status != 200) return this.$message.error(res.meta.msg)
      this.recruitlist = res.data.records
      this.total = res.data.total
      console.log(this.recruitlist)
    },
    //监听pagesize改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getrecruitlist()
    },
    //监听页码值改变的数据
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getrecruitlist()
    },
    //监听添加招聘对话框的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields()
    },
    goAddpage() {
      this.$router.push('/Recruitadd')
    },
    //点击按钮，添加新招聘
    addrecruit() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) {
          return
        }
        const { data: res } = await this.$http.post('recruit', this.addForm)
        if (res.meta.status != 201) {
          this.$message.error(res.meta.msg)
        }
        this.$message.success(res.meta.msg)
        //隐藏
        this.addDialogVisible = false
        this.getrecruitlist()
      })
    },

    //展示招聘编辑的对话框
    async showEditDialog(id) {
      const { data: res } = await this.$http.get('recruit/' + id)
      if (res.meta.status != 200) {
        return this.$message.error('获取信息错误')
      }
      const form = _.cloneDeep(res.data)
      
      form.placeids=form.placeids.split(",")
      var placeids = new Array()
      for (let index = 0; index < 3; index++) {
        placeids.push(parseInt(form.placeids[index]))
      }
      form.placeids=placeids

      form.cateids=form.cateids.split(",")
      var cateids = new Array()
      for (let index = 0; index < 3; index++) {
        cateids.push(parseInt(form.cateids[index]))
      }
      form.cateids=cateids
      
      this.editForm = form
      this.editDialogVisible = true
    },
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },
    //修改招聘信息
    editrecruitInfo() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return
        const form = _.cloneDeep(this.editForm)
          form.placeids = form.placeids.join(',')
          form.cateids = form.cateids.join(',')
        //发起请求
        const { data: res } = await this.$http.put(
          'recruit/' + form.id,
          form
        )
        if (res.meta.status != 200) {
          this.$message.error('修改招聘信息失败')
        }
        this.editDialogVisible = false
        this.getrecruitlist()
        this.$message.success('修改招聘信息成功')
      })
    },
    //删除招聘
    async removeUserById(id) {
      //弹窗
      const confirmResult = await this.$confirm(
        '此操作将永久删除该招聘, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)
      //如果招聘确定删除则返回字符串 confirm
      //如果招聘取消 则cancel
      if (confirmResult != 'confirm') {
        return this.$message.info('已经取消删除')
      }
      const { data: res } = await this.$http.delete('recruit/' + id)
      if (res.meta.status != 200) {
        return this.$message.error('删除招聘失败')
      }
      this.$message.success('删除招聘成功')
      this.getrecruitlist()
    },
  },
}
</script>

<style lang="less" scoped>
.demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>